import linecache
import os
from util import file
import random

# 当前文件所在的文件夹
dirname = os.path.dirname(__file__)

def get_random_useragent():
    """获取爬虫用的随机useragent
    原理: 读取'./useragent.list'文件中随机一行并返回, 同时剔除行尾的换行符
    """
    cnt = file.get_file_lines(dirname + './useragent.list')
    return linecache.getline(dirname + './useragent.list', random.randint(1, cnt)).strip()

# python3 -m crawl_helper.fake_useragent
if __name__ == '__main__':
    for i in range(10):
        print(get_random_useragent())